<%= render(:partial => "shared/flash_message") %>
<%= render :partial => "shared/config_save_actions", :locals => {:scope => {}} -%>
<%= form_for(scope[:material],
            :as => :material,
            :url => scope[:url],
            :html => {:method => scope[:method],
                      :onsubmit => "return AjaxForm.jquery_ajax_submit(this);",
                      :class => "popup_form"}) do |f| %>

    <%= md5_field %>
    <%= current_tab_field("materials") -%>
    <%= hidden_field_tag :material_type, scope[:material].getType() %>
    <div class="new_form_content change_materials">
        <%- if repository_packages_map_from_config.empty? -%>
            <p class="warnings">No repositories found. Please <a href='<%=package_repositories_new_path%>'>add a package repository</a> first.</p>
        <%- end -%>

        <div class="new_form_item_block vtop" id="repository_div">
            <div class="new_form_label">
                <%= required_label(f, "repository", 'Repository') -%>
                <div class="inline_instruction">Choose a configured repository</div>
            </div>
            <div class="new_form_control required">
                <%= select_tag "material[package_definition[repositoryId]]", [], {:id => "repository"} %>
            </div>
            <div id="repository_error"></div>
        </div>

        <div class="new_form_item checkbox_row">
            <div class="new_form_label">
                <%= required_label(f, com.thoughtworks.go.config.materials.PackageMaterialConfig::PACKAGE_ID, 'Package') -%>
            </div>
            <div class="new_form_control required">
                <div class="checkbox_row_inline">
                    <%= radio_button_tag "material[create_or_associate_pkg_def]", "associate", true, :id => "chooseExisting" %>
                    <label for="chooseExisting"><%= "Choose Existing" -%></label>
                </div>

                <div class="checkbox_row_inline">
                    <%= radio_button_tag "material[create_or_associate_pkg_def]", "create", false, :id => "addNew" %>
                    <label for="addNew"><%= "Define New" -%></label>
                </div>
            </div>
        </div>

        <div class="new_form_item">
            <div class="new_form_label"></div>
            <div class="new_form_control">
                <%= f.select com.thoughtworks.go.config.materials.PackageMaterialConfig::PACKAGE_ID, [], {}, {:id => "package"} -%>
                <%= error_message_on(scope[:material], com.thoughtworks.go.config.materials.PackageMaterialConfig::PACKAGE_ID, :css_class => "form_error") %>
            </div>
        </div>

        <%- state = (params[:material] && params[:material][:create_or_associate_pkg_def]) ? params[:material][:create_or_associate_pkg_def] : '' -%>
        <div class="package_definition">
            <div class="package_configuration">
                <%- if (state == "associate" || state == "") -%>
                    <%= render :partial => 'admin/package_definitions/show_package_definition', :locals => {:scope => {:package_configuration => @package_configuration}} %>
                <%- else -%>
                    <%= render :partial => 'admin/package_definitions/form', :locals => {:scope => {:package_configuration => @package_configuration}} %>
                <%- end -%>
            </div>
        </div>
        <%= render :partial => 'shared/form_required_message.html', :locals => {:scope => {}} %>
    </div>
    <%= render :partial => "shared/form_buttons.html", :locals => {:scope => {:submit_label => scope[:submit_label]}} unless scope[:read_only_page] %>
    <%= render :partial => 'shared/convert_tool_tips.html', :locals => {:scope => {}} %>
<% end %>

<script type="text/javascript">
    var data = <%== repository_packages_map_from_config.to_json %>;
    var repo = <%== scope[:material].getPackageDefinition() && scope[:material].getPackageDefinition().getRepository() ? scope[:material].getPackageDefinition().getRepository().getId().to_json  : "".to_json %>;
    var pkg = <%== scope[:material].getPackageDefinition() ? scope[:material].getPackageDefinition().getId().to_json : "".to_json %>;
    var initialState = <%== (params[:material] && params[:material][:create_or_associate_pkg_def]) ? params[:material][:create_or_associate_pkg_def].to_json : "".to_json %>;
    var showPackageDefPath = <%== package_definitions_show_path(:repo_id => "$repoId$", :package_id => "$packageId$").to_json %>;
    var newPackageDefPath = <%== package_definitions_new_path(:repo_id => "$repoId$").to_json %>;
    var pluginMissingErrorMessage = <%== "Associated plugin ''{0}'' not found. Please contact the Go admin to install the plugin.".to_json %>;
    Util.on_load(function () {
        new PackageMaterialCheckConnection("<%= package_definition_check_connection_path %>").bind(".new_form_content.change_materials", "#check_package", "#package_check_message");
        new PackageMaterialDefinition(data, repo, pkg, showPackageDefPath, newPackageDefPath, pluginMissingErrorMessage).init(initialState);
        if (<%= scope[:read_only_page] || false %>) {
          Util.disableAllFormElementsFor('.change_materials');
        }
    });
</script>
